console.log("preload js loaded")
const userAgents = [
  'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
  'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)',
  'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20',
  'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6',
  'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER',
  'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0) ,Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
  'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
  'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)',
  'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)',
  'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre',
  'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52',
  'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
  'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)',
  'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6',
  'Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6',
  'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)',
  'Opera/9.25 (Windows NT 5.1; U; en), Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
  'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
]
var Crawler = require("crawler")
var uuid = require('short-uuid')
var tiankongzy = require('./tiankongzy')
var xiangkanzy = require('./xiangkanzy')
console.log('tiankongzy', tiankongzy)
console.log('xiangkanzy', xiangkanzy)
var c = new Crawler({
  maxConnections : 10,
  encoding:'utf8',
  userAgent: userAgents[Math.fround(Math.random() * (userAgents.length-1))],
  // This will be called for each crawled page
  callback : function (error, res, done) {
      if(error){
          console.log(error);
      }else{
          var $ = res.$;
          let listData = {
            _id: res.options.id,
            name: res.options.name,
            list: []
          }
          switch (res.options.source) {
            case 'tiankongzy':
              eval(tiankongzy.list)
              break;
            case 'xiangkanzy':
              eval(xiangkanzy.list)
              break;
            default:
              break;
          }
          // $('.xing_vb ul li').each(function(index, item) {
          //   let vodName = $(item).find('.xing_vb4').text()
          //   let vodUrl = $(item).find('.xing_vb4 a').attr('href')
          //   // let vodAddr = $(item).find('.container').find('li').eq(1).text()
          //   let vodType = $(item).find('.xing_vb5').text()
          //   let vodDate = $(item).find('.xing_vb7').text()
          //   if (vodName) {
          //     let data = {
          //       _id: uuid.generate(),
          //       vodName,
          //       vodUrl,
          //       // vodAddr,
          //       vodType,
          //       vodDate
          //     }
          //     listData.list.push(data)
          //   }
          // })
          utools.db.put(listData)
          // console.log($("title").text());
      }
      done();
  }
})

window.getSearch = function(data) {
  console.log('data', data)
  let id = 'search/'+uuid.generate()
  c.queue({
    id: id,
    uri: data.url,
    name: data.name,
    source: data.source,
  })
  return id
}

window.getMovie = function(data) {
  console.log(data)
  let id = 'history/'+uuid.generate()
  detail.queue({
    id,
    uri: data.url,
    source: data.source,
  })
  return id
}

var detail = new Crawler({
  maxConnections : 10,
  encoding:'utf8',
  userAgent: userAgents[Math.fround(Math.random() * (userAgents.length-1))],
  // This will be called for each crawled page
  callback : function (error, res, done) {
      if(error){
          console.log(error);
      }else{
          var $ = res.$;
          console.log(res)
          // let poster = $('.vodBox img').attr('src')
          // let name = $('.vodBox .vodh h2').text() + $('.vodBox .vodh span').text()
          // let douban = $('.vodBox .vodh label').text()
          // let alias =$('.vodBox .vodinfobox li').eq(0).text()
          // let director = $('.vodBox .vodinfobox li').eq(1).text()
          // let starring = $('.vodBox .vodinfobox li').eq(2).text()
          // let categories = $('.vodBox .vodinfobox li').eq(3).text()
          // let area =$('.vodBox .vodinfobox li').eq(4).text()
          // let lang =$('.vodBox .vodinfobox li').eq(5).text()
          // let release =$('.vodBox .vodinfobox li').eq(6).text()
          // let date =$('.vodBox .vodinfobox li').eq(7).text()
          // let introduction = $('.playBox').eq(0).children('.vodplayinfo').text()
          // console.log($("title").text());
          // let links = []
          // $('.playBox').eq(1).find('ul').eq(1).children('li').each(function(index, item) {
          //   links.push($(item).text())
          // })
          // let data = {
          //   _id: res.options.id,
          //   poster,
          //   name,
          //   douban,
          //   director,
          //   starring,
          //   categories,
          //   area,
          //   lang,
          //   release,
          //   date,
          //   alias,
          //   introduction,
          //   links
          // }
          let data = {}
          switch (res.options.source) {
            case 'tiankongzy':
              eval(tiankongzy.detail)
              break;
            case 'xiangkanzy':
              eval(xiangkanzy.detail)
              break;
            default:
              break;
          }
          if (data.name) {
            utools.db.put(data)
          }
      }
      done();
  }
});